<template>
	<view class="page">
		<!-- 头部背景 -->
		<view class="head-bg">
			<view class="head-user">
				<view class="user">
					<image :src="userInfo.avatar" mode=""></image>
					<text>{{userInfo.username}}</text>
				</view>
				<view class="exchange" @click="isRule = true">
					<text>规则说明</text>
					<text class="iconfont icon-more"></text>
				</view>
			</view>
			<view class="integral-balance">
				<view class="integral"   >
					<text>累计获得积分</text>
					<text class="number">{{integral}}</text>
					<text class="btn" @click="onInte" >补签</text>
					<text class="btn" @click="onIntegral"  :class="{'action':SigninStatus===1}">{{SigninStatus===0?'立即签到':'已签到'}}</text>
				</view>
			</view>
			<view class="bg">
				<image src="/static/integral_bg1.png" mode=""></image>
			</view>
		</view>
		<!-- 签到 -->
		<view class="Signin-data">
			<view class="signin-title">
				<view class="title">
					<text>每日签到</text>
				</view>
			</view>
			<view class="day-list">
				<view class="list" v-for="(item,index) in deploy" 
				:class="{'action':index + 1 <= sign_sum}"
				:key="index">
					<view class="circle">
						<text v-show="index + 1 > sign_sum">+{{item.earn_point}}</text>
						<text class="iconfont icon-duihao" v-show="index + 1 <= sign_sum"></text>
					</view>
					<view class="day">
						<text>{{index+1}}天</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 签到提示弹窗 -->
		<view class="sigin-hint">
			<view class="cu-modal" :class="{'show':isSignin}">
			  <view class="cu-dialog">
			    <view class="cu-bar bg-white justify-end">
			      <view class="content">签到成功</view>
			      <view class="action">
			        <text class="cuIcon-close text-red" @click="isSignin = false"></text>
			      </view>
			    </view>
			    <view class="sigin-content">
						<view class="icon">
							<text class="iconfont icon-signin"></text>
						</view>
						<view class="title">
							<view>恭喜您，连续签到<text>{{sign_sum + 1}}</text>天，获得<text v-for="(item,index) in deploy" v-if="(sign_sum + 1) == item.sign_num"> {{item.earn_point}}</text>积分</view>
						</view>
						<view class="hint">
							<text>连续签到7天可获得额外积分奖励</text>
						</view>
						<view class="btn" @click="isSignin = false">
							<text>我知道了</text>
						</view>
					</view>
			  </view>
			</view>
		</view>
		<!-- 签到提示弹窗 -->
		<view class="sigin-hint">
			<view class="cu-modal" :class="{'show':isRetroactive}">
			  <view class="cu-dialog">
			    <view class="cu-bar bg-white justify-end">
			      <view class="content">补签成功</view>
			      <view class="action">
			        <text class="cuIcon-close text-red" @click="isRetroactive = false"></text>
			      </view>
			    </view>
			    
			  </view>
			</view>
		</view>
		<!-- 规则说明弹窗 -->
		<view class="rule-win">
			<view class="cu-modal" :class="{'show':isRule}">
			  <view class="cu-dialog">
			    <view class="cu-bar bg-white justify-end">
			      <view class="content">规则说明</view>
			      <view class="action">
			        <text class="cuIcon-close text-red" @click="isRule = false"></text>
			      </view>
			    </view>
			    <view class="rule-content">
						<text>
							1.签到第1天送10积分，连续签到第2天送20积分，以此类推，连续签到第7天可获额外积分奖励。第8天签到则重新开始-个七日轮回，视为首日签到;若签到中断则重新计算;
						</text>
					</view>
			  </view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isRetroactive:false,
				isSignin: false,
				SigninStatus: 0,
				// 规则
				isRule: false,
				sign_sum: 0,
				day:0,
				deploy:[],
				userInfo:[],
				integral:uni.getStorageSync("integral")
			};
		}, 
		methods:{
			/**
			 * 签到点击
			 */
			onIntegral(){
				if(this.SigninStatus == 1){
					return false;
				}else{
					this.$http.post('user/sign', {
					}).then(res => {
						if(res.data.code == 200){
							this.isSignin = true;
							this.SigninStatus = 1; 
							this.$http.get('user/integral', {
							}).then(res => {
								if(res.data.code == 200){
									console.log(res.data)
									uni.setStorageSync("integral",res.data.data.integral)
									this.integral = res.data.data.integral
								}
							})
						}
						
					})
				}
			},
			onInte(){
				this.$http.post('user/retroactive', {
				}).then(res => {
					if(res.data.code == 200){
						uni.showToast({
							title:"补签成功",
							icon:"none",
							duration:1500
						})
					}
				})
			},
		},
		onLoad() {
			this.$http.get('user/integral', {
			}).then(res => {
				if(res.data.code == 200){
					uni.setStorageSync("integral",res.data.data.integral)
					this.integral = res.data.data.integral
				}
			})
			//签到配置
			this.$http.get('sign/deployList', {
			}).then(res => {
				if(res.data.code == 200){
					this.day = res.data.data.length
					this.deploy = res.data.data
				}
			})
		},
		onReady() {
			this.userInfo = uni.getStorageSync('userInfo');
			this.integral = this.userInfo.integral
			this.$http.get('user/isSign', {
			}).then(res => {
				if(res.data.data.SigninStatus == 1){
					this.SigninStatus = res.data.data.SigninStatus
					if(res.data.data.sign_sum == 7){
						this.sign_sum = 0
					}
					this.sign_sum = res.data.data.sign_sum
				}else{
					this.SigninStatus = res.data.data.SigninStatus
					if(res.data.data.sign_sum == 7){
						this.sign_sum = 0
					}
					this.sign_sum = res.data.data.sign_sum
				}
			})
		}
	}
</script>

<style scoped lang="scss">
	@import 'SignIn.scss';
</style>
